package com.xcity.game.guild.operation;

import com.xcity.db.entity.Guild;
import com.xcity.db.entity.GuildMember;
import com.xcity.db.entity.Player;
import com.xcity.game.common.AbstractConfirmOperation;
import com.xcity.game.common.MessageConst;
import com.xcity.game.guild.GuildChannel;
import com.xcity.game.guild.GuildService;
import com.xcity.game.guild.gevent.GuildEvent;
import com.xcity.game.player.cache.PlayerCacheService;
import com.xcity.pomelo.push.SyncPush;
import com.xcity.pomelo.push.msg.TipPush;
import com.xcity.pomelo.push.sync.IntSync;
import com.xcity.pomelo.response.GuildTransferResponse;
import com.xcity.pomelo.response.HintResponse;

import naga.x.App;

/**
 * 公会转让确认操作
 * @author shipeng.zhu
 */
public class GuildTransferOperation extends AbstractConfirmOperation {

	protected GuildMember member;
	protected int serial;
	protected long targetId;
	
	public GuildTransferOperation(Player ref, int serial, GuildMember member, long targetId, int timeoutMillis, String msg) {
		super(ref, timeoutMillis, msg);
		this.serial = serial;
		this.member = member;
		this.targetId = targetId;
	}

	@Override
	public void ok(Player player) {
		GuildService gs = App.getApp().getServiceManager().get(GuildService.class);
		
		Guild guild = null;
		if(member.getGuildid() <= 0){
			player.send(new HintResponse(serial, MessageConst.GUILD_NOT_IN_GUILD));
			return;
		}
		guild = gs.findGuildById(member.getGuildid());
		if(guild == null){
			player.send(new HintResponse(serial, MessageConst.GUILD_NOT_IN_GUILD));
			return;
		}
		if(member.getDuty() != GuildService.MEMBER_DUTY_TYPE_CHAIRMAN){
			player.send(new HintResponse(serial, MessageConst.GUILD_NO_RIGHT_TO_TRANS));
			return;
		}
		GuildMember targetMember = gs.getGuildMember(targetId);
		if(targetMember == null){
			targetMember = gs.loadGuildMember(targetId);
			if(targetMember == null){
				player.send(new HintResponse(serial, MessageConst.GUILD_NO_THIS_MEMBER));
				return;
			}
		}
		if(targetMember.getGuildid() != guild.getId()){
			player.send(new HintResponse(serial, MessageConst.GUILD_NO_SAME_GUILD));
			return;
		}
		if(targetMember.getDuty() != GuildService.MEMBER_DUTY_TYPE_OFFICIAL){
			player.send(new HintResponse(serial, MessageConst.GUILD_MUST_BE_OFFIICIAL));
			return;
		}
		Player targetPlayer = App.getApp().getServiceManager().get(PlayerCacheService.class).find(targetId);
		if(targetPlayer == null){
			player.send(new HintResponse(serial, MessageConst.GUILD_NO_PLAYER));
			return;
		}
		
		
		member.setDuty(GuildService.MEMBER_DUTY_TYPE_OFFICIAL);
		targetMember.setDuty(GuildService.MEMBER_DUTY_TYPE_CHAIRMAN);
		guild.setChairmanId(targetMember.getId());
		player.send(new TipPush(MessageConst.GUILD_TRANSFER_OK));
		player.send(new GuildTransferResponse(serial, targetPlayer.getId()));//, MessageConst.GUILD_TRANSFER_OK
		if(targetPlayer.isOnline()){
			targetPlayer.send(new TipPush(MessageConst.GUILD_TRANSFER_BEING_CHAIRMAN));
		}
		guild.getGevent().addNewGuildEvents(GuildEvent.EVENT_TYPE_TRANS, player.getId(), targetId);
		gs.toTransLog(player.getId(), guild.getId(), targetPlayer.getId());
		//其他成员同步
		gs.sendSyncForGuildChannel(player.getId(), guild.getId(), GuildChannel.GUILD_DETAIL_CHANGE_SYNC | GuildChannel.GUILD_MEMBER_CHANGE_SYNC | GuildChannel.GUILD_EVENT_CHANGE_SYNC);
	}
	
	@Override
	public void cancel(Player player){
		//取消
		player.send(new HintResponse(serial, MessageConst.GUILD_OPERATION_CANCEL));
	}

}
